﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SignDetection
{
    public struct Vector
    {
        public Geometry Head;
        public Geometry Tail;
    }
    public partial class ColorClassiffierForm : Form
    {
        
        Bitmap ori_bitmap;
        Bitmap cls_bitmap;
        Bitmap RGB_bitmap;

        int R = 255;
        int G = 255;
        int B = 255;
        int XLENGTH;// =;// YLENGTH*Math.Cos(Math.PI/3);
        int YLENGTH;//=(int)(256*Math.Sqrt(2))
        public ColorClassiffierForm()
        {
            InitializeComponent();

            YLENGTH = (int)(256 * Math.Sqrt(2));
            XLENGTH = (int)(YLENGTH * Math.Sin(Math.PI / 3));
            RGB_bitmap = new Bitmap(500, 20);
            ori_bitmap = new Bitmap(256, 256);

            R = 255;
            Geometry point_E;
            for (B = 0; B < 256; B++)
                for (G = 0; G < 256; G++)
                {
                    point_E.b = 255 * B / (double)(R + G + B);
                    point_E.g = 255 * G / (double)(R + G + B);
                    point_E.r = 255 * R / (double)(R + G + B);
                    ori_bitmap.SetPixel((int)point_E.b, (int)point_E.g, Color.FromArgb(R, G, B));
                }
            G = 255;
            for (R = 0; R < 256; R++)
                for (B = 0; B < 256; B++)
                {
                    point_E.b = 255 * B / (double)(R + G + B);
                    point_E.g = 255 * G / (double)(R + G + B);
                    point_E.r = 255 * R / (double)(R + G + B);
                    ori_bitmap.SetPixel((int)point_E.b, (int)point_E.g, Color.FromArgb(R, G, B));
                }
            B = 255;
            for (R = 0; R < 256; R++)
                for (G = 0; G < 256; G++)
                {
                    point_E.b = 255 * B / (double)(R + G + B);
                    point_E.g = 255 * G / (double)(R + G + B);
                    point_E.r = 255 * R / (double)(R + G + B);
                    ori_bitmap.SetPixel((int)point_E.b, (int)point_E.g, Color.FromArgb(R, G, B));
                }

            pictureBox1.Refresh();
            pictureBox1.Image = ori_bitmap;
        }

        private void ColorClassiffierForm_Load(object sender, EventArgs e)
        {
            //ColorClasiffier colorCls=new ColorClasiffier();
            //pictureBoxColCls.Image = colorCls.ColorPlane();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "jpg files(*.jpg)|*.jpg|all files(*.*)|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            if (dlg.FileNames.Length == 1)
            {
                cls_bitmap = new Bitmap(dlg.FileName);
                pictureBox2.Image = cls_bitmap;
                pictureBox2.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap bitmap = Histogram.Histo(cls_bitmap, XLENGTH, YLENGTH);
                pictureBox4.Image = bitmap;
                pictureBox4.Refresh();
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
            //Graphics g = Graphics.FromImage(bitmap);
            //for (int i = 0; i < 256;i++ )
            //    g.DrawLine(new Pen(Brushes.DeepSkyBlue),new Point(0, 0), new Point(Gray_Hist[i], 0));
            //pictureBox4.Refresh();
            //pictureBox4.Image = bitmap;


        }
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Location.X >= 0 && e.Location.X < 256)
                if (e.Location.Y >= 0 && e.Location.Y < 256 - e.Location.X)
                {

                    Geometry point_E;
                    point_E.b = e.Location.X;
                    point_E.g = e.Location.Y;

                    point_E.r = 255 - point_E.b - point_E.g;

                    B = 255;
                    {
                        R = (int)(point_E.r * B / point_E.b);
                        G = (int)(point_E.g * B / point_E.b);
                        if (R < 256 && G < 256)
                        {
                            for (int i = 0; i < 500; i++)
                                for (int j = 0; j < 20; j++)
                                {
                                    int index = i / 10;
                                    int r = index * R / 50;
                                    int g = index * G / 50;
                                    int b = index * B / 50;
                                    RGB_bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));

                                }
                            pictureBox3.Image = RGB_bitmap;
                            pictureBox3.Refresh();
                            textBox1.Text = R.ToString();
                            textBox2.Text = G.ToString();
                            textBox3.Text = B.ToString();
                            return;
                        }

                    }
                    G = 255;
                    {
                        R = (int)(point_E.r * G / point_E.g);
                        B = (int)(point_E.b * G / point_E.g);
                        if (R < 256 && B < 256)
                        {
                            for (int i = 0; i < 500; i++)
                                for (int j = 0; j < 20; j++)
                                {
                                    int index = i / 10;
                                    int r = index * R / 50;
                                    int g = index * G / 50;
                                    int b = index * B / 50;
                                    RGB_bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));

                                }
                            pictureBox3.Image = RGB_bitmap;
                            pictureBox3.Refresh();
                            textBox1.Text = R.ToString();
                            textBox2.Text = G.ToString();
                            textBox3.Text = B.ToString();

                            return;
                        }

                    }
                    R = 255;
                    {
                        B = (int)(point_E.b * R / point_E.r);
                        G = (int)(point_E.g * R / point_E.r);
                        if (B < 256 && G < 256)
                        {
                            for (int i = 0; i < 500; i++)
                                for (int j = 0; j < 20; j++)
                                {
                                    int index = i / 10;
                                    int r = index * R / 50;
                                    int g = index * G / 50;
                                    int b = index * B / 50;
                                    RGB_bitmap.SetPixel(i, j, Color.FromArgb(r, g, b));

                                }
                            pictureBox3.Image = RGB_bitmap;
                            pictureBox3.Refresh();
                            textBox1.Text = R.ToString();
                            textBox2.Text = G.ToString();
                            textBox3.Text = B.ToString();
                            return;
                        }

                    }


                }


        }

        private void ColorClassiffierForm_Load_1(object sender, EventArgs e)
        {

        }
    }
}
